﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Data.OleDb;
using Persons.Interfaces;
using Persons.DS;
using Persons.Classes;
using System.IO;

namespace Persons.Tests
{
    [TestFixture]
    public class UT_ListOfPersons
    {
        List<Persona> expected;
        ListOfPersons actual;
        Persona temp;
        private static string[] myListOfFiles =
            {
                 @"D:\2012\HW\HW33\ListOfPersons\Persons\Files\personaCSV.csv",
                 @"Provider=SQLNCLI10.1;Data Source=JULIA-PC\MSSQLHEDGEHOG;Integrated Security=SSPI;Initial Catalog=ListOfPersona"
                
            };
        private static IDS[] listOfIPerson =
                {
                    new DS_CSV(myListOfFiles[0]), 
                    new DS_DB(myListOfFiles[1]),
                    new DS_MOCK(null)
                };
        [SetUp]
        public void Init()
        {
            OleDbConnectionStringBuilder stringBuilder = new OleDbConnectionStringBuilder();
            stringBuilder.ConnectionString = @"Provider=SQLNCLI10.1;Data Source=JULIA-PC\MSSQLHEDGEHOG;Integrated Security=SSPI;Initial Catalog=ListOfPersona";
            OleDbConnection _connectionSQL = new OleDbConnection(stringBuilder.ConnectionString);
            DS_DB.Connection = _connectionSQL;
            _connectionSQL.Open();
            OleDbCommand command = _connectionSQL.CreateCommand();
            try
            {
                command.CommandText =
                    "DROP TABLE [dbo].[Persona]";
                command.ExecuteNonQuery();
            }
            catch
            {
            }
            finally
            {
                command.CommandText =
                    "CREATE TABLE [dbo].[Persona]([id_key] [int] IDENTITY(1,1) NOT NULL, [ID] [int]  NOT NULL, [Fname] [nvarchar](50) NULL, [Lname] [nvarchar](50) NULL, [Age] [int] NULL) ON [PRIMARY]";
                command.ExecuteNonQuery();
                _connectionSQL.Close();
            }
        }
        private void InitPer(int _type, ref IDS ds)
        {
            //Init();
            FileStream actual_file = new FileStream(myListOfFiles[0], FileMode.Create);
            actual_file.Close();
            actual = new ListOfPersons();
            expected = new List<Persona>();
            if (ds.GetType().Name == "DS_MOCK")
            {
                ds = new DS_MOCK();
            }
            switch (_type)
            {
                case 0:
                    {
                        break;
                    }
                case 1:
                    {
                        InitOne();
                        ds.Create(actual[0]);
                        break;
                    }
                case 2:
                    {
                        InitMoreThanOne();
                        foreach (Persona p in actual)
                        {
                            ds.Create(p);
                        }
                        break;
                    }
            }

        }
        private void InitMoreThanOne()
        {
            temp = new Persona(1, "Ivan", "Ivanov", 4, null);
            actual.Add(temp);
            temp = new Persona(10, "Petr", "Petrov", 23, null);
            actual.Add(temp);
            temp = new Persona(5, "Sidor", "Sidoriv", 17, null);
            actual.Add(temp);
            temp = new Persona(2, "Chyvak", "Chyvakov", 45, null);
            actual.Add(temp);
            temp = new Persona(3, "Adam", "Lambert", 78, null);
            actual.Add(temp);
            temp = new Persona(4, "Jessie", "J", 29, null);
            actual.Add(temp);
            temp = new Persona(6, "Sonya", "Sonyavna", 55, null);
            actual.Add(temp);
            temp = new Persona(7, "Kate", "Lavric", 32, null);
            actual.Add(temp);
        }
        private void InitOne()
        {
            temp = new Persona(1, "Ivan", "Ivanov", 4,null);
            actual.Add(temp);
        }

        [Test, TestCaseSource("listOfIPerson")]
        public void TestForCreateReadOne(IDS _ds)
        {
            InitPer(1, ref _ds);
            ListOfPersons.ds = _ds;
            actual.Create(new Persona(12, "Gregory", "House", 53,null));
            ListOfPersons loadlist = new ListOfPersons();
            loadlist.Read();
            Assert.AreEqual(true, actual.Compare(loadlist));
        }
        [Test, TestCaseSource("listOfIPerson")]
        public void TestForCreateReadMoreThanOne(IDS _ds)
        {
            InitPer(2, ref _ds);
            ListOfPersons.ds = _ds;
            actual.Create(new Persona(12, "Gregory", "House", 53,null));
            ListOfPersons loadlist = new ListOfPersons();
            loadlist.Read();
            Assert.AreEqual(true, actual.Compare(loadlist));
        }
        [Test, TestCaseSource("listOfIPerson")]
        public void TestForCreateReadNullList(IDS _ds)
        {
            InitPer(0, ref _ds);
            ListOfPersons.ds = _ds;
            actual.Create(new Persona(12, "Gregory", "House", 53,null));
            ListOfPersons loadlist = new ListOfPersons();
            loadlist.Read();
            Assert.AreEqual(true, actual.Compare(loadlist));
        }
        [Test, TestCaseSource("listOfIPerson")]
        public void TestForReadID(IDS _ds)
        {
            InitPer(2, ref _ds);
            ListOfPersons.ds = _ds;
            ListOfPersons loadlist = new ListOfPersons();
            loadlist.Read(4);
            actual = new ListOfPersons();
            actual.Add(new Persona(4, "Jessie", "J", 29,null));
            Assert.AreEqual(true, actual.Compare(loadlist));
        }
        [Test, TestCaseSource("listOfIPerson")]
        public void TestForDeleteID(IDS _ds)
        {
            InitPer(2, ref _ds);
            ListOfPersons.ds = _ds;
            ListOfPersons loadlist = new ListOfPersons();
            loadlist.Delete(4);
            loadlist.Read();
            actual.RemoveAt(5);
            Assert.AreEqual(true, actual.Compare(loadlist));
        }
        [Test, TestCaseSource("listOfIPerson")]
        public void TestForUpdatePersona(IDS _ds)
        {
            InitPer(2, ref _ds);
            ListOfPersons.ds = _ds;
            ListOfPersons loadlist = new ListOfPersons();
            loadlist.Update(new Persona(4, "Jessie", "J", 19,null));
            loadlist.Read();
            actual[5].Age = 19;
            Assert.AreEqual(true, actual.Compare(loadlist));
        }
    }
}
